package com.spzx.product.controller;

import com.github.pagehelper.PageInfo;
import com.spzx.domain.Brand;
import com.spzx.product.service.IBrandService;
import com.spzx.vo.Result;
import com.spzx.vo.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @Operation(summary = "获取品牌详细信息")
    @GetMapping("/brand/{id}")
    public Result<Brand> getById(@PathVariable Long id) {
        Brand brand = brandService.getById(id);
        return Result.build(brand, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分页查询品牌信息
     * @param page
     * @param limit
     * @param brandName
     * @return
     */
    @Operation(summary = "获取品牌分页列表")
    @GetMapping("/brand/findPage/{page}/{limit}")
    public Result<PageInfo<Brand>> list(
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable Integer page,
            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @PathVariable Integer limit,
            @Parameter(name = "brandName", description = "品牌名称", required = false)
            @RequestParam(required = false, defaultValue = "") Integer brandName) {

        PageInfo<Brand> brand = brandService.list(page, limit, brandName);
        return Result.build(brand, ResultCodeEnum.SUCCESS);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @Operation(summary = "新增品牌")
    @PostMapping("/brand")
    public Result add(@RequestBody @Validated Brand brand) {
        int rows = brandService.save(brand);
        return Result.build(rows, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @Operation(summary = "修改品牌")
    @PutMapping("brand")
    public Result update(@RequestBody @Validated Brand brand) {
        int rows = brandService.update(brand);
        return Result.build(rows, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除品牌
     * @param brand
     * @return
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("brand/{ids}")
    public Result delete(@RequestBody @Validated Brand brand) {
        int rows = brandService.delete(brand);
        return Result.build(rows, ResultCodeEnum.SUCCESS);
    }

}
